// AutoCADModelObserver.cpp: implementation of the AutoCADModelObserver class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AutoCADModelObserver.h"
#include "UEEntityOperation.h"
#include <dbdict.h>
#include <dbxrecrd.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

const CString AutoCADModelObserver::strMatrixName = _T("EntityMatrix");
const CString AutoCADModelObserver::strHandleName = _T("EntityHandle");
const CString AutoCADModelObserver::strTypeName = _T("EntityType");
const CString AutoCADModelObserver::strType = _T("SupportHanger");
const CString AutoCADModelObserver::strSHIndexName = _T("SH_Index");
const CString AutoCADModelObserver::strPartIndexName = _T("SH_Part_Index");

AutoCADModelObserver::AutoCADModelObserver()
{
	m_Matrix.setToIdentity();
	m_handle.setNull();
}

AutoCADModelObserver::~AutoCADModelObserver()
{
/*	for(int i = 0; i < m_theReators.GetSize(); i++)
	{
		delete m_theReators.GetAt(i);
	}*/
}

void AutoCADModelObserver::AddReactorToEntity(AcDbEntityReactor* pReactor)
{
/*	ASSERT(!m_handle.isNull());

	AcDbEntity* pEnt = CUEEntityOperation::OpenEntityWrite(m_handle);
	ASSERT(pEnt);
	pEnt->addReactor(pReactor);
	pEnt->close();
	m_theReators.Add(pReactor);*/
}

void AutoCADModelObserver::DisableUndoRecording(Adesk::Boolean disable)
{
}

AcDbDictionary* AutoCADModelObserver::GetDictionary()
{
	return m_DicHandle.GetDictionary();
}

CString AutoCADModelObserver::GetValue(const CString& strName)
{
/*	AcDbObjectId id;
	AcDbDictionary* pDic = GetDictionary();
	ASSERT(pDic);
	AcDbObject* pObj = NULL;
	AcDbXrecord* pRcd = NULL;
	Acad::ErrorStatus es = pDic->getAt(strName, pObj, AcDb::kForRead);
	if(pObj == NULL)
	{
		pDic->close();
		return CString();
	}
	pDic->close();
	if(!pObj->isKindOf(AcDbXrecord::desc()))
	{
		ASSERT(FALSE);
		return CString();
	}
	pRcd = static_cast<AcDbXrecord*>(pObj);

	resbuf* pbuf = NULL;
	pRcd->rbChain(&pbuf);
	if(pbuf == NULL)
	{
		ASSERT(FALSE);
		return CString();
	}
	ASSERT(pbuf->restype == AcDb::kDxfXdAsciiString);
	
	CString strTemp(pbuf->resval.rstring);
	acutRelRb(pbuf);

	return strTemp;*/

	CString strTemp;
	m_DicHandle.GetValue(strName, strTemp);
	return strTemp;
}

void AutoCADModelObserver::SetValue(const CString& strName, const CString& strValue)
{
	m_DicHandle.SetValue(strName, strValue);
/*	AcDbObjectId id;
	AcDbDictionary* pDic = GetDictionary();
	ASSERT(pDic);
	AcDbObject* pObj = NULL;
	AcDbXrecord* pRcd = NULL;
	Acad::ErrorStatus es = pDic->getAt(strName, pObj, AcDb::kForRead);
	if(pObj == NULL)
	{
		pObj = new AcDbXrecord;
		es = pDic->setAt(strName, pObj, id);
	}
	pDic->close();
	if(!pObj->isKindOf(AcDbXrecord::desc()))
	{
		ASSERT(FALSE);
		return;
	}

	pRcd = static_cast<AcDbXrecord*>(pObj);

	resbuf buf;
	buf.restype = AcDb::kDxfXdAsciiString;
	buf.resval.rstring = (TCHAR*)malloc(strValue.GetLength() + 1);
	CString strTemp;
	strTemp = strValue;
	memcpy(buf.resval.rstring, strTemp.GetBuffer(0), (strValue.GetLength() + 1)*sizeof(TCHAR));
	strTemp.ReleaseBuffer();
	
	es = pRcd->setFromRbChain(buf);

	free(buf.resval.rstring);
	pRcd->close();*/
}

AutoCADModelObserver& AutoCADModelObserver::operator=(const AutoCADModelObserver& other)
{
	if(this == &other)
		return *this;

	this->m_DicHandle = other.m_DicHandle;
	this->m_handle = other.m_handle;
	this->m_Matrix = other.m_Matrix;
	this->m_strName = other.m_strName;
	this->m_pObj = other.m_pObj;

	return *this;
}

BOOL AutoCADModelObserver::IsEntityExist()
{
	const AcDbHandle& handle = EntityHandle();

	AcDbObject* pObj = CUEEntityOperation::OpenObjectRead(handle);
	if(pObj == NULL)
		return FALSE;
	pObj->close();
	return TRUE;
}

void AutoCADModelObserver::RemoveSubDictionary(const CString& strName, Dictionary& dic)
{
	AcDbDictionary* pDic = dic.GetDictionary();
	ASSERT(pDic);

	pDic->remove(strName);
	pDic->close();
}